package cn.mxz.robots.basic.remote;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.ConcurrentHashMap;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

/**
 * 省份学信息
 * 
 * @author cuizhennan<cuizhennan@mouge.wang>
 * @since 2015-06-24
 */
public final class ProvinceFactory
{

	private static Logger LOG = Logger.getLogger(ProvinceFactory.class);

	final static ConcurrentHashMap<String, String> provinceMap = new ConcurrentHashMap<String, String>();

	static
	{
		provinceMap.put("1", "Anhui-安徽");
		provinceMap.put("2", "Beijing-北京");
		provinceMap.put("3", "Chongqing-重庆");
		provinceMap.put("4", "Fujian-福建");
		provinceMap.put("5", "Gansu-甘肃");
		provinceMap.put("6", "Guangdong-广东");
		provinceMap.put("7", "Guangxi-广西");
		provinceMap.put("8", "Guizhou-贵州");
		provinceMap.put("9", "Hainan-海南");
		provinceMap.put("10", "Hebei-河北");
		provinceMap.put("11", "Henan-河南");
		provinceMap.put("12", "Heilongjiang-黑龙江");
		provinceMap.put("13", "Hubei-湖北");
		provinceMap.put("14", "Hunan-湖南");
		provinceMap.put("15", "Jilin-吉林");
		provinceMap.put("16", "Jiangsu-江苏");
		provinceMap.put("17", "Jiangxi-江西");
		provinceMap.put("18", "Liaoning-辽宁");
		provinceMap.put("19", "InnerMongolia-内蒙古");
		provinceMap.put("20", "Ningxia-宁夏");
		provinceMap.put("21", "Qinghai-青海");
		provinceMap.put("22", "ShandongNew-山东");
		provinceMap.put("23", "Shanxi-山西");
		provinceMap.put("24", "Shaanxi-陕西");
		provinceMap.put("25", "Shanghai-上海");
		provinceMap.put("26", "Sichuan-四川");
		provinceMap.put("27", "Tianjin-天津");
		provinceMap.put("28", "Tibet-西藏");
		provinceMap.put("29", "Xinjiang-新疆");
		provinceMap.put("30", "Yunnan-云南");
		provinceMap.put("31", "Zhejiang-浙江");
	}

	public static String chooseProvice(String provinceId)
	{

		String className = provinceMap.get(provinceId).split("-")[0];

		if ("".equals(className) || className == null)
		{
			className = standByProvince(provinceId);

			LOG.warn("provinceId:" + provinceId + "==>" + provinceMap.get(provinceId));
		}

		LOG.info("provinceId:" + provinceId + "==>" + provinceMap.get(provinceId));
		return className;
	}

	/**
	 * 备用方案
	 */
	private static String standByProvince(String provinceId)
	{
		JSONObject province = JSONObject.fromObject(readerFile());

		String typeClass = province.getJSONObject(provinceId).getString("typeClass");

		return typeClass;
	}

	private static String readerFile()
	{
		ClassLoader classLoader = AbstractExam.class.getClassLoader();
		InputStream in = classLoader.getResourceAsStream("cn/mxz/robots/basic/remote/province.json");

		BufferedReader reader = null;
		String laststr = "";
		try
		{
			InputStreamReader inputStreamReader = new InputStreamReader(in, "UTF-8");
			reader = new BufferedReader(inputStreamReader);
			String tempString = null;
			while ((tempString = reader.readLine()) != null)
			{
				laststr += tempString;
			}

			reader.close();
			inputStreamReader.close();
			in.close();
		}
		catch (IOException e)
		{
			LOG.error("project Gaokao robots class ProvinceFactory method 【readerFile】 IOException." + e.getMessage());
		}
		finally
		{
			if (reader != null)
			{
				try
				{
					reader.close();
				}
				catch (IOException e)
				{
					LOG.error("project Gaokao robots class ProvinceFactory method 【readerFile】 close stream IOException." + e.getMessage());
				}
			}
		}

		return laststr;
	}
}
